<?php defined('_JEXEC') or die('Restricted access'); ?><?php defined('_SEXEC') or die('Restricted access'); ?>
<?php
if (!isset($_SESSION['username']) && !isset($_SESSION['usertype']))
    header("location: index.php");
?>
<?php
if (isset($submit)) {
    switch (trim($submit)) {
        case 'Save':
            b($bill_date, "Select the Bill Date");
            b($period_from, "Select the Period From Date");
            b($period_to, "Select the Period To Date");
            if (count($arr) == 0) {
                $bill_date = db_date_format($bill_date);
                $period_from = db_date_format($period_from);
                $period_to = db_date_format($period_to);
                $sql = "insert into bills (company_id,document_id,bill_no,bill_date,period_from,period_to,datetime) values(\"$company_id\",\"$document_id\",\"$bill_no\",\"$bill_date\",\"$period_from\",\"$period_to\",now())";
                mysql_query($sql);

                $sql = "update companies set invoice_no = invoice_no + 1 where id = '" . $company_id . "'";
                mysql_query($sql);

                $msg = "Bill added succesful";
                $company_id = $document_id = $bill_no = $bill_date = $period_from = $period_to = "";
            }
            break;
        case "Update":
            if ($step == 2) {
                b($bill_date, "Select the Bill Date");
                b($period_from, "Select the Period From Date");
                b($period_to, "Select the Period To Date");
                if (count($arr) == 0) {
                    $bill_date = db_date_format($bill_date);
                    $period_from = db_date_format($period_from);
                    $period_to = db_date_format($period_to);
                    $sql = "update bills set company_id=\"$company_id\", document_id=\"$document_id\", bill_date=\"$bill_date\", period_from=\"$period_from\", period_to=\"$period_to\" where id = '$id'";
                    mysql_query($sql);
                    $msg = "Bill updated succesful";
                    $company_id = $document_id = $bill_no = $bill_date = $period_from = $period_to = "";
                } else {
                    $btval = "     Update     ";
                }
            } else if (isset($eid)) {
                $sql = "select * from bills where id = '$eid'";
                $result = mysql_query($sql);
                $row = mysql_fetch_array($result);
                $id = $row['id'];
                $company_id = $row['company_id'];
                $document_id = $row['document_id'];
                $bill_no = $row['bill_no'];
                $bill_date = $row['bill_date'];
                $period_from = $row['period_from'];
                $period_to = $row['period_to'];
                $bill_date = db_date_format($bill_date);
                $period_from = db_date_format($period_from);
                $period_to = db_date_format($period_to);
                $step = 2;
                $btval = "     Update     ";
            }
            break;
        case "Delete":
            $sql = "delete from bills where id = '$eid'";
            mysql_query($sql);
            $msg = "Bill delete succesful";
            break;
    }
}

if (!isset($btval))
    $btval = "    Save    ";

$sql = "select id, companyname, companycode, invoice_no from companies where status = 1 order by companyname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $company_arr[$row['id']] = $row['companyname'];
    $company_invoice_arr[$row['id']] = $row['companycode'] . ' / '; //.$row['invoice_no'];
}
?>

<script>

    function generate_invoice()
    {
        company = document.getElementById("company_id").value;
        var company_invoice = new Array()
<?php
foreach ($company_invoice_arr as $k => $v) {
    echo "company_invoice['" . $k . "'] = '" . $v . "';";
}
?>
        document.getElementById("bill_no").value = company_invoice[company];
        document.getElementById("invoice_no").innerHTML = company_invoice[company];
    }



</script>

<div id="title">Report Management</div>

<form name="report-form" method="post" action="">
    <table border="0" cellspacing="3" cellpadding="3" align="center">
        <tr>
            <td colspan=4>
<?php
se();
?>
            </td>
        </tr>
        <tr>
            <td align=right>Company Name:</td>
            <td colspan=3>
<?php
if (!isset($submit) || $company_id == "") {
    ?>
                    <select name="company_id" id="company_id" onchange="generate_invoice()">
                    <?php
                    sopt($company_arr, $company_id)
                    ?>
                    </select>
                        <?php
                    } else {
                        echo '<b>' . $company_arr[$company_id] . '</b>';
                        echo '<input name="company_id" id="company_id" type="hidden" value="' . $company_id . '" />';
                    }
                    ?>

            </td>
        </tr>
        <tr>
            <td align=right>Bill Type:</td>
            <td colspan=3>
                <select name="document_id" id="document_id">
<?php
sopt($slab_type_arr, $document_id)
?>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan=4>&nbsp;</td>
        </tr>
        
        <tr>
            <td align=right>Period From:</td>
            <td><input name="period_from" type="text" value="<?php echo $period_from ?>" id="period_from" onfocus="popcal(this)" autocomplete="off" /></td>
            <td align=right>To:</td>
            <td><input name="period_to" type="text" value="<?php echo $period_to ?>" id="period_to" onfocus="popcal(this)" autocomplete="off" /></td>
        </tr>
        <tr>
            <td align=right>Bill No.:</td>
            <td>
                <b><span id="invoice_no"><?php echo $bill_no ?></span></b>
                <input name="bill_no" id="bill_no" type="hidden" value="<?php echo $bill_no ?>" />
            </td>
            <td align=right>Bill Date:</td>
            <td><input name="bill_date" id="bill_date" type="text" value="<?php echo $bill_date ?>" onfocus="popcal(this)" autocomplete="off" /></td>
        </tr>
        <tr>
            <td colspan="4" align="center">
                <input type=hidden name=id value="<?php echo $id ?>" />
                <input type=hidden name=step value="<?php echo $step ?>" />
                <input type="submit" name="submit" value="<?php echo $btval ?>" class='button'/>
            </td>
        </tr>	
    </table>
    <script>
        $(function() {
            $('#bill_date').focus(function() {
                $billo = $('#bill_no').val();
                $billno1= $billo.split('/');
                $date = $('#period_to').val();
                var res = $date.split("-");
                if (res[1] != undefined) {
                    $billDate = res[1] + "" + res[2];
                    $bill_no = $billno1[0]+"/"+ $billDate;
                    $('#invoice_no').text($bill_no);
                    $('#bill_no').val($bill_no);
                }
            });
        });
    </script>
</form>

<?php
if (!isset($submit) || $bill_no == "") {
    ?>
    <script> generate_invoice();</script>
    <?php
}
?>

<?php
echo "<hr />";
$table = "bills";
$cols = "id, company_id as `Company`, document_id as `Bill Type`, bill_no, date_format(bill_date, '%d-%m-%Y') as `Bill Date`, date_format(period_from, '%d-%m-%Y') as `Period From`, date_format(period_to, '%d-%m-%Y') as `Period To`";
$orderby = ($orderby == '') ? "id" : $orderby;
$sort = ($sort == '') ? "desc" : $sort;
$pagelength = 50;
$reparr = array();
foreach ($slab_type_arr as $k => $v) {
    $reparr['Bill Type'][$k] = $v;
}
if (is_array($company_arr)) {
    foreach ($company_arr as $k => $v) {
        $reparr['Company'][$k] = $v;
    }
}
gettable("$table", $cols, "$where", "$orderby", "$sort", $pg, "<a href='print.php?bill_id=\$id' target='_blank' style='text-decoration:none'>View</a>&nbsp;|<a href='downlaod.php?bill_id=\$id' target='_blank' style='text-decoration:none'>Download</a>&nbsp;|&nbsp;<a href='?eid=\$id&submit=Update' style='text-decoration:none'>Update</a>&nbsp;|&nbsp;<a href=\"?eid=\$id&submit=Delete\" onclick=\"javascript:return confirm('Are you sure you want to Delete?')\" style='text-decoration:none'>Delete</a>", Array("id"), $reparr);
?>